package com.biz.controller.userCenter;

import com.biz.model.Pmodel.base.PbaseUser;
import com.biz.model.Pmodel.base.PmyApplyActivity;
import com.biz.service.MService.user.UserServiceI;
import com.biz.utils.ConfigUtil;
import com.framework.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lzq.
 */
@Controller
@RequestMapping(value="/userCenter")
public class UserCenterController extends BaseController{

    @Resource(name = "userService")
    private UserServiceI userService;


    /**
     * 跳转个人中心
     * @throws Exception
     */
    @RequestMapping(value="/toUserCenter")
    public ModelAndView toUserCenter()throws Exception{
        ModelAndView mv = new ModelAndView ();
        String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
        String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));

        PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);

        mv.addObject("userId", userId);
        mv.addObject("pbaseUser",pbaseUser);
        mv.setViewName("userCenter/userCenter");
        return mv;
    }


    /**
     * 跳转我的申请
     * @throws Exception
     */
    @RequestMapping(value="/toMyApplyList")
    public ModelAndView toMyApplyList()throws Exception{
        ModelAndView mv = new ModelAndView ();
        String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));

        List<PmyApplyActivity> actlist = userService.getUserApplyListByUserId(userId);
        mv.addObject("userId", userId);
        mv.addObject("actlist",actlist);
        mv.setViewName("userCenter/myApplyList");

        return mv;
    }


    /**
     * 加载数量
     * @throws Exception
     */
    @RequestMapping(value="/loadCount")
    public void loadCount(HttpServletResponse response)throws Exception{
        String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));

        Map<String,Object> actlist = userService.getCountByUserId(userId);

        writeJson(actlist,response);
    }

    /**
     * 判断该手机号是否被绑定
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryRegInfoByPhone")
    public void queryRegInfoByPhone(String phone,HttpServletResponse response)
            throws Exception{
        String result="";
        Map<String, String> map = new HashMap<String, String>();
        if(userService.queryRegInfoByphone(phone)==null){
            map.put("result", "nouser");
        }else{
            map.put("result", "oneuser");
        }
       writeJson(map,response);
    }

    /**
     * 跳转个人资料
     */
    @RequestMapping(value="/toUserInfo")
    public ModelAndView toUserInfo()throws Exception{
        ModelAndView mv = new ModelAndView ();
        String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));

        PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);

        mv.addObject("pbaseUser",pbaseUser);
        mv.setViewName("userCenter/userInfo");

        return mv;
    }


    /**
     * 跳转个人资料修改
     */
    @RequestMapping(value="toEditDetail")
    public ModelAndView toEditDetail()throws Exception{
        ModelAndView mv = new ModelAndView ();
        String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));

        PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);

        mv.addObject("page_type","1");//返回个人资料页面 /userCenter/toUserInfo.ac
        mv.addObject("pbaseUser",pbaseUser);
        mv.setViewName("userCenter/editUserInfo");
        return mv;
    }

    /**
     * 修改绑定手机号
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateRegister")
    public void updateRegister(String phone,String userName,HttpServletRequest request,HttpServletResponse response)throws Exception{
        String userId = String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
        Map<String, String> map = new HashMap<String, String>();
        HttpSession session = this.getRequest().getSession();
        map =userService.updateRegister(userId,userName,phone,session);
        writeJson(map,response);
    }


    /**
     * 跳转积分累计页面
     */
    @RequestMapping(value="toInteralTotal")
    public ModelAndView toInteralTotal()throws Exception{
        ModelAndView mv = new ModelAndView ();
        String userId = String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
        List<Map<String, String>> intralList = userService.getInteralTotalListByUserId(userId);
        String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));

        PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);

        mv.addObject("pbaseUser",pbaseUser);
        mv.addObject("intralList",intralList);
        mv.setViewName("userCenter/userIntegralTotal");
        return mv;
    }
}
